package com.thingsgrid.device.controller;

import com.thingsgrid.device.entity.Function;
import com.thingsgrid.device.service.IFunctionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.tool.api.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@AllArgsConstructor
@RequestMapping("/function")
@Api(value = "", tags = "接口")
public class FunctionController {
	@Autowired
	private IFunctionService functionService;


	/**
	 * 新增
	 */
	@PostMapping("/submit")
	@ApiOperation(value = "新增", notes = "传入function", position = 1)
	public boolean save(@Valid @RequestBody String[] ids,String productId) {
		functionService.saveFunction(ids,productId);
		return true;
	}

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperation(value = "删除", notes = "传入productId和Function对象", position = 2)
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String productId,@RequestBody Function functional) {
		return R.status(functionService.removeFunction(productId, functional));
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	@ApiOperation(value = "修改", notes = "传入function", position = 3)
	public void update(@Valid @RequestBody Function function) {
		functionService.updateFunction(function);
	}

	/**
	 * 查找所有
	 */
	@GetMapping("/selectAll")
	@ApiOperation(value = "查询所有", notes = "", position = 4)
	public R<List<Function>> selectAll() {
		return R.data(functionService.findAll());
	}

	/**
	 * 根据functionName查信息
	 */
	@GetMapping("/detail")
	@ApiOperation(value = "查询一个", notes = "", position = 5)
	public Function selectById(@ApiParam(value = "functionId", required = true) @RequestParam String functionId) {
		return functionService.findFunctionByName(functionId);
	}

	/**
	 * 分页查询
	 */
	@GetMapping("/list")
	@ApiOperation(value = "分页", notes = "传入页数和每页显示的条数", position = 6)
	public R<Map<String, Object>> pageList(@ApiParam(value = "页数和条数", required = true)
								   @RequestParam Integer pageIndex, Integer pageSize,String productId,String physicalName) {
       return R.data(functionService.findAll(pageIndex, pageSize,productId,physicalName));

    }
	/**
	 * 根据productId查功能
	 */
@GetMapping("/functionDetail")
	public R<List<Function>> findFunctionByProductId(@ApiParam(value = "productId", required = true) @RequestParam String productId){
	return R.data(functionService.findFunctionByProductId(productId));
}
}
